<?php
class db{
	public $conn = NULL;
	public $result = NULL;
	public $host="localhost";
	public $user="root";
	public $pass="@#tin*";
	public $database="cwl";
	private $author="Trần Hữu Trung Tín";
	protected $version="0.1";

	function __construct(){
		$this->conn = mysql_connect($this->host, $this->user, $this->pass);
		mysql_select_db($this->database, $this->conn);
		mysql_query("SET NAMES 'utf8'");
	}//End fucntion __construct()
	
	function getData($sql){
		$this->result = mysql_query($sql) or die(mysql_error());
	}//End function getData()
	
	function fetchRow(){
		if(is_resource($this->result)==false)
			return false;
		$row = mysql_fetch_assoc($this->result);
		return $row;
	}//End function fetchRow()
	
	function TheLoai($lang='vi' , $AnHien=1){
		$sql="SELECT idTL, TenTL from theloai WHERE (AnHien=$AnHien or $AnHien=-1)
			AND (lang = '$lang' or '$lang'='')
			ORDER BY ThuTu";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//TheLoai

	function TheLoaiChonLoc($lang='vi' , $AnHien=1){
		$sql="SELECT idTL, TenTL from theloai WHERE (AnHien=$AnHien or $AnHien=-1)
			AND (lang = '$lang' or '$lang'='') AND idTL<>24 AND idTL<>2 AND idTL<>25 AND idTL<>26
			ORDER BY ThuTu";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//TheLoai
	
	function LoaiTin ($lang='vi',$AnHien=1){
		$sql="SELECT idLT, Ten  FROM loaitin
			WHERE (AnHien = $AnHien or $AnHien = -1) 
		AND (lang = '$lang' or '$lang'='') 
			ORDER BY Thutu";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	} //LoaiTin	
	
	function MotLoaiTin ($idLT, $lang='vi',$AnHien=1){
		settype($idTL, "int");	
		$sql="SELECT idLT, Ten  FROM loaitin
			WHERE (AnHien = $AnHien or $AnHien = -1) 
			AND idLT = $idLT
			AND (lang = '$lang' or '$lang'='') 
			ORDER BY Thutu";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	} //End Function MotLoaiTin

	function LoaiTinTrongTheLoai($idTL, $lang='vi', $AnHien=1){
		settype($idTL, "int");	
		$sql = "SELECT idLT, Ten FROM loaitin  WHERE (AnHien=$AnHien or $AnHien=-1)  
		  AND	idTL=$idTL  AND ( lang ='$lang' or '$lang'='' )
		  ORDER BY ThuTu ASC";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	}//end function LoaiTinTrongTheLoai
	
	function LoaiTinTrongTheLoaiMoi($idTL, $lang='vi', $AnHien=1){
		settype($idTL, "int");	
		$sql="SELECT loaitin.idLT, loaitin.Ten, theloai.TenTL
			FROM  loaitin, theloai
			WHERE loaitin.idTL=theloai.idTL AND loaitin.idTL=$idTL";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	}//end function LoaiTinTrongTheLoai
	
	//-----------------------------------LOAI TIN CON------------------------------------------//
	
	//-----------------------------------END LOAI TIN CON------------------------------------------//
	
	function TheLoaiTrongNgonNgu($lang, $AnHien=1){
		//settype($idTL, "int");	
		$sql = "SELECT idTL, TenTL FROM theloai  WHERE (lang=$lang or $AnHien=-1)  
				ORDER BY ThuTu ASC";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	}//end function TheLoaiTrongNgonNgu

	function changeTitle($str){
		$str = $this->stripUnicode($str);
		$str = str_replace("?","",$str);
		$str = str_replace("&","",$str);
		$str = str_replace("'","",$str);		
		$str = str_replace("  "," ",$str);
		$str = trim($str);
		$str = mb_convert_case($str , MB_CASE_TITLE , 'utf-8');
	// MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
		$str = str_replace(" ","-",$str);	
		return $str;
	}
	
	function changeTitle_space($str){
                $str = $this->stripUnicode($str);
                $str = str_replace("?","",$str);
                $str = str_replace("&","",$str);
                $str = str_replace("'","",$str);
                $str = str_replace("  "," ",$str);
                $str = trim($str);
                $str = mb_convert_case($str , MB_CASE_TITLE , 'utf-8');
        // MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
                $str = str_replace(" "," ",$str);       
                return $str;
        }

	function stripUnicode($str){
		if(!$str) return false;
		$unicode = array(
		 'a'=>'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
		 'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
		 'd'=>'đ',
		 'D'=>'Đ',
		 'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
		 'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
		 'i'=>'í|ì|ỉ|ĩ|ị',	  
		 'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
		 'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
		 'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
		 'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
		 'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
		 'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
		 'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
		);
		foreach($unicode as $khongdau=>$codau) {
		  $arr = explode("|",$codau);
		  $str = str_replace($arr,$khongdau,$str);
		}
		return $str;
	}//end function stripUnicode
	
	public function DemSoLienHe($idLienHe=-1){
		$sql="SELECT count(*) FROM lienhe 
			WHERE (idLienHe=$idLienHe OR $idLienHe=-1)
			";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
	}//end function DemSoTinTrong1Loai
	
	public function DemSoTinTrong1Loai($idTL=-1, $idLT=-1, $lang='vi', $AnHien=-1){
		$sql="SELECT count(*) FROM  tin 
			WHERE (idTL=$idTL OR $idTL=-1) AND (idLT=$idLT OR $idLT=-1) 
			AND (lang='$lang' OR '$lang'='') AND (AnHien=$AnHien OR $AnHien=-1) ";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
	}//end function DemSoTinTrong1Loai

	public function DemSoTinTrong1TheLoai($idTL=-1, $lang='vi', $AnHien=-1){
		$sql="SELECT count(*) FROM  loaitin 
			WHERE (idTL=$idTL OR $idTL=-1) 
			AND (lang='$lang' OR '$lang'='') AND (AnHien=$AnHien OR $AnHien=-1) ";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
	}//end function DemSoTrong1TheLoai
	
	function pagesLink($totalRows, $pageNum=1, $pageSize=5){	
		$baseURL = $_SERVER['PHP_SELF'];
		parse_str($_SERVER['QUERY_STRING'],$arr);
		unset($arr['pageNum']);		
		foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
		$baseURL .="?".$str;
					
		if ($totalRows<=0) return "";
		$totalPages = ceil($totalRows/$pageSize);
		if ($totalPages<=1) return "";
						
		$firstLink="";  $prevLink="";  $lastLink="";  $nextLink="";
		
		if ($pageNum > 1) {
			$firstLink = "<a href='$baseURL'>Trang đầu</a>";
			$prevPage = $pageNum - 1;
			$prevLink="<a href='$baseURL&pageNum=$prevPage'>Trang trước</a>";
		}
		if ($pageNum < $totalPages) { 
			$lastLink = "<a href='$baseURL&pageNum=$totalPages'>Trangcuối</a>";
			$nextPage = $pageNum + 1;
			$nextLink = "<a href='$baseURL&pageNum=$nextPage'>Trang kế</a>";
		} 
		$str_phantrang = " Bạn đang ở trang ".$pageNum." ";
		return $firstLink.$prevLink.$str_phantrang.$nextLink.$lastLink;
	}//end function pagesLink

	function pagesList($totalRows , $pageNum=1, $pageSize = 5, $offset = 5){
		$baseURL = $_SERVER['PHP_SELF'];
		parse_str($_SERVER['QUERY_STRING'],$arr);
		unset($arr['pageNum']);		
		foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
		$baseURL .="?".$str;
	
		if ($totalRows<=0) return "";
		$totalPages = ceil($totalRows/$pageSize);
		if ($totalPages<=1) return "";
	
		$from = $pageNum - $offset;	
		$to = $pageNum + $offset;
		if ($from <=0) { $from = 1;   $to = $offset*2; }
		if ($to > $totalPages) { $to = $totalPages; }
		$links = "";
		for($j = $from; $j <= $to; $j++) {
			//Định dạng cho trang hiện tại
			if($j==$pageNum) $links = $links."<span class='pageListSpan'>$j</span>";
			//-----------------------------
			else
				$links= $links . "<span id=pageList><a href = '$baseURL&pageNum=$j'>$j</a></span>"; 
		} //for
		return $links;
	} //end function pagesList
	
	function pagesListLink($totalRows, $pageNum=1, $pageSize=5, $offset=3){	
		$baseURL = $_SERVER['PHP_SELF'];
		parse_str($_SERVER['QUERY_STRING'],$arr);
		unset($arr['pageNum']);		
		foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
		$baseURL .="?".$str;
					
		if ($totalRows<=0) return "";
		$totalPages = ceil($totalRows/$pageSize);
		if ($totalPages<=1) return "";
						
		$firstLink="";  $prevLink="";  $lastLink="";  $nextLink="";
		
		if ($pageNum > 1) {
			$firstLink = "<span id='first'><a href='$baseURL'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></span>";
			$prevPage = $pageNum - 1;
			$prevLink="<span id='prev'><a href='$baseURL&pageNum=$prevPage'>&nbsp;&nbsp;&nbsp;&nbsp;</a></span>";
		}
		if ($pageNum < $totalPages) { 
			$lastLink = "<span id='last'><a href='$baseURL&pageNum=$totalPages'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></span>";
			$nextPage = $pageNum + 1;
			$nextLink = "<span id='next'><a href='$baseURL&pageNum=$nextPage'>&nbsp;&nbsp;&nbsp;&nbsp;</a></span>";
		} 
		$str_phantrang = " Bạn đang ở trang ".$pageNum." ";
		return $firstLink.$prevLink.$this->pagesList($totalRows,$pageNum,$pageSize,$offset).$nextLink.$lastLink;
	}//end function pagesListLink //code của PageLink

	public function CountWardInOneDistrict($idDistrict=-1, $AnHien=-1){
                $sql="SELECT count(*) FROM  ward
                        WHERE (idDistrict=$idDistrict OR $idDistrict=-1)
                        AND (AnHien=$AnHien OR $AnHien=-1) ";
                $rs = mysql_query($sql) or die(mysql_error());
                $rows_rs = mysql_fetch_row($rs);
                $totalRows = $rows_rs[0];
                return $totalRows;
        }//end function CountWardInOneDistrict
		
	function District($AnHien=1){
		$sql="SELECT idDistrict, name from district WHERE (AnHien=$AnHien or $AnHien=-1)
			ORDER BY ThuTu";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//District

	function WardInDistrict($idDistrict, $AnHien=1){
		settype($idDistrict, "int");	
		$sql = "SELECT idWard, name FROM ward WHERE (AnHien=$AnHien or $AnHien=-1)  
		  AND	idDistrict=$idDistrict
		  ORDER BY ThuTu ASC";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	}//end function LoaiTinTrongTheLoai
	
	function StreetInWard($idWard, $AnHien=1){
		//settype($idDistrict, "int");	
		settype($idWard, "int");	
		$sql = "SELECT idStreet, name FROM street WHERE (AnHien=$AnHien or $AnHien=-1)  
		        AND idWard=$idWard
      		    ORDER BY idStreet ASC";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
	}//end function StreetInWard
}
?>
